//
//  ATTextView.m
//  Postre
//
//  Created by CoderLT on 2017/6/4.
//  Copyright © 2017年 CoderLT. All rights reserved.
//

#import "ATTextView.h"

@implementation ATTextView
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
- (void)setup {
    // 1.默认配置
    self.scrollIndicatorInsets = UIEdgeInsetsMake(4.0f,0.0f,4.0f,1.0f);
    self.contentInset = UIEdgeInsetsZero;
    self.scrollEnabled = YES;
    self.scrollsToTop = NO;
    self.userInteractionEnabled = YES;
    self.font = [UIFont systemFontOfSize:16.0f];
    self.textColor = [UIColor colorWithRGB:0x333333];
    self.backgroundColor = [UIColor whiteColor];
    self.keyboardAppearance = UIKeyboardAppearanceDefault;
    self.keyboardType = UIKeyboardTypeDefault;
    self.returnKeyType = UIReturnKeyDone;
    self.textAlignment = NSTextAlignmentLeft;
    _placeholderMutiLines = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}
- (void)textDidChange {
    [self setNeedsDisplay];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 重写uitextView 父类方法
- (void)setTextAlignment:(NSTextAlignment)textAlignment{
    [super setTextAlignment:textAlignment];
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font{
    [super setFont:font];
    [self setNeedsDisplay];
}

- (void)insertText:(NSString *)text{
    [super insertText:text];
    [self setNeedsDisplay];
}

- (void)setContentInset:(UIEdgeInsets)contentInset {
    [super setContentInset:contentInset];
    [self setNeedsDisplay];
}

- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
    [super setTextContainerInset:textContainerInset];
    [self setNeedsDisplay];
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    [self setNeedsDisplay];
}

- (void)setPlaceholder:(NSString *)placeholder{
    if([placeholder isEqualToString:_placeholder]) {
        return;
    }
    _placeholder = placeholder;
    [self setNeedsDisplay];
}

- (void)setPlaceholderMutiLines:(BOOL)placeholderMutiLines
{
    _placeholderMutiLines = placeholderMutiLines;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if ([self.text length] == 0 && self.placeholder){
        UIColor *placeholderColor = [UIColor colorWithWhite:0.7 alpha:1.0];
        CGRect placeHolderRect = CGRectMake(7.0f + self.textContainerInset.left,
                                            self.textContainerInset.top,
                                            rect.size.width - 2.0f - self.textContainerInset.left - self.textContainerInset.right,
                                            rect.size.height - self.textContainerInset.top - self.textContainerInset.right);
        [placeholderColor set];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        if (!_placeholderMutiLines) {
            paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
        }
        paragraphStyle.alignment = self.textAlignment;
        
        [self.placeholder drawInRect:placeHolderRect
                      withAttributes:@{ NSFontAttributeName : self.font,
                                        NSForegroundColorAttributeName : placeholderColor,
                                        NSParagraphStyleAttributeName : paragraphStyle }];
    }
}

@end
